/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import org.apache.commons.logging.LogFactory;
import org.xptracker.util.UserConfigString;

import java.io.File;
import java.io.IOException;
import java.util.Set;

public class FileStructureBootstrapper {
  @Inject
  @Named("file-dependencies")
  private Set<UserConfigString> dependencies;

  public void bootstrap() {
    try {
      for (UserConfigString dependency : dependencies) {
        File f = new File(dependency.get());
        makeDirs(f);
      }
    } catch (Exception e) {
      LogFactory.getLog(getClass()).error("Error attempting file structure creation", e);
    }
  }

  private void makeDirs(File dir) throws IOException {
    if (!dir.exists()) {
      String fullPath = dir.getAbsolutePath();
      LogFactory.getLog(getClass()).info("Attempting to create: " + fullPath);
      if (!dir.mkdirs()) {
        throw new IOException("Unable to create directory: " + fullPath);
      }
    }
  }
}
